package com.atguigu.wc.transform;

import com.atguigu.wc.beans.SensorReading;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @Author:GaoFei
 * @Description:
 * @Date:Created in 18:11
 * @Modified By:
 */
public class ReduceTransform3 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> stringDataStreamSource = env.readTextFile("D:\\1BIGDATA\\spark-streaming\\Flink\\src\\main\\resources\\hello.txt");
        env.setParallelism(1);
        SingleOutputStreamOperator<SensorReading> map = stringDataStreamSource
                .map(line -> {
                    String[] split = line.split(",");
                    return new SensorReading(split[0], new Long(split[1]), new Double(split[2]));
                });
        //                       定义的分组字段类型
        KeyedStream<SensorReading, Tuple> id = map.keyBy("id");
        id.print();
        SingleOutputStreamOperator<SensorReading> reduce = id.reduce((v1, v2) -> new SensorReading(v1.getId(), v2.getTimestamp(), Math.max(v1.getTemperature(), v2.getTemperature())));
        env.execute();
    }
}
